package ext.generic.integration.manually;

import java.util.Locale;

import org.apache.commons.lang.StringUtils;

import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationResultSet;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.tianma.part.forms.TMTypeUtil;
import wt.fc.Persistable;
import wt.fc.WTReference;
import wt.part.WTPart;
import wt.util.WTException;

public class SecondaryBomManuallyPublishValidator extends DefaultUIComponentValidator{
	
	public static boolean checkDesignView(Object object)throws WTException {
		boolean flag = false;
		if (object == null) {
			return flag;
		}

		if ((object instanceof WTPart)) {
			WTPart part = (WTPart) object;
			String view = part.getViewName();
			if (StringUtils.isNotBlank(view) && StringUtils.equals("Design", view)) {
				flag = true;
				return flag;
			}
		}
		return flag;
	} 
	@Override
	public UIValidationResultSet performFullPreValidation( UIValidationKey uivalidationKey , UIValidationCriteria uivalidationCriteria, Locale locale ) throws WTException {
		UIValidationResultSet uivresultSet = new UIValidationResultSet();
		UIValidationStatus uivalidationstatus = UIValidationStatus.HIDDEN;
		boolean flag = false;
		
		if (uivalidationCriteria != null && uivalidationCriteria.getContextObject() != null) {
			WTReference wtreference = uivalidationCriteria.getContextObject();
			if(null == wtreference){
				uivresultSet.addResult( new UIValidationResult(uivalidationKey, uivalidationstatus ) );
				return uivresultSet;
			}
			Persistable persistable = wtreference.getObject();

			WTPart part = null;			
			if(persistable instanceof WTPart)
			{			
				part = (WTPart) persistable;
			}
			if(checkDesignView(part)) {
				flag = true ;
			}
		}
		if (flag) {
			uivresultSet.addResult(new UIValidationResult(uivalidationKey, UIValidationStatus.ENABLED));
		} else {
			uivresultSet.addResult(new UIValidationResult(uivalidationKey, UIValidationStatus.HIDDEN));
		}
		
		return uivresultSet;
	}

}
